local life $life

local ave_panels $ave_panels

local r $r

local KW_per_panel=$KW_per_panel

local pollutants co2 nox pm25 so2

local scc_vals 41 148 152 330


local suffix  -transmission-low // -transmission-high
*local suffix -own-region

* https://news.energysage.com/how-many-solar-panels-do-i-need/





foreach scc of local scc_vals  {


	clear

	import delimited using `"../Data/electricity-generation/output/damage-per-mwh-oge-clean-coal-scc-`scc'.csv"'

	rename plant_id_eia orispl_code

	gen ProdMedianCC=net_generation_mwh_split
	gen DamagesCoef1CC=md_per_mwh_low
	gen DamagesCoef2CC=md_per_mwh_high

	save  ../Data/CleanedData/PPDamagesCC`scc', replace 

	clear

	import delimited using `"../Data/electricity-generation/output/damage-per-mwh-oge-scc-`scc'.csv"'

	rename plant_id_eia orispl_code

	save  ../Data/CleanedData/PPDamages`scc', replace 
	
	*sum md_per_mwh_low

}



clear

import delimited using `"../Data/electricity-generation/output/PPRegressors`suffix'-oge.csv"'

save  ../Data/CleanedData/PPRegressors`suffix', replace 




clear

import delimited using `"../Data/electricity-generation/output/PPCap-oge.csv"'

merge 1:1 orispl_code using  ../Data/CleanedData/PPRegressors`suffix'

keep if _merge==3

foreach scc of local scc_vals  {

drop _merge

merge 1:1 orispl_code  using ../Data/CleanedData/PPDamages`scc'

 
rename md_per_mwh_low DamagesCoef1`scc'
rename md_per_mwh_high DamagesCoef2`scc'

sum  DamagesCoef1`scc'


gen DamagesCoef1_alt_`scc'=0
gen DamagesCoef2_alt_`scc'=0

foreach pollutant of local pollutants {

	rename md_per_mwh_low_`pollutant' DamagesCoef1_`pollutant'_`scc'
	rename md_per_mwh_high_`pollutant' DamagesCoef2_`pollutant'_`scc'
	
	replace DamagesCoef1_alt_`scc'=DamagesCoef1_alt_`scc'+DamagesCoef1_`pollutant'_`scc'
	replace DamagesCoef2_alt_`scc'=DamagesCoef2_alt_`scc'+DamagesCoef2_`pollutant'_`scc'
	
}
 


tab _merge



keep if _merge==3

}
gen ProdMedian=net_generation_mwh_split

gen PPCap=namepcap
gen PPInt=intercept




local counter=1
foreach nercname in cal mro npcc rfc serc tre wecc {
	gen PPRegion_r`counter'=excess_load_`nercname'
	gen PPRegion2_r`counter'=excess_load_`nercname'_sq
	
	outsheet PPRegion_r`counter' using ../Data/CleanedData/PPRegion`suffix'_r`counter'.csv, replace nonames
	outsheet PPRegion2_r`counter' using ../Data/CleanedData/PPRegion2`suffix'_r`counter'.csv, replace nonames	
	
	gen PPInf_r`counter'=est_extremum_`nercname'
	
	replace PPInf_r`counter'=0 if  PPInf_r`counter'==.
	outsheet PPInf_r`counter' using ../Data/CleanedData/PPInf`suffix'_r`counter'.csv, replace nonames	



	
	local counter=`counter'+1
}


gen PPScale=exp(log_scale)


drop _merge
merge 1:1 orispl_code  using ../Data/CleanedData/PPDamagesCC148
*save  ../Data/CleanedData/PPDamagesCC, replace 
rename DamagesCoef1CC DamagesCoef1CC148
rename DamagesCoef2CC DamagesCoef2CC148



foreach var in PPCap PPInt  PPScale  ProdMedian   ProdMedianCC  DamagesCoef1CC148 DamagesCoef2CC148 {
	outsheet `var' using ../Data/CleanedData/`var'`suffix'.csv, replace nonames
}




foreach scc of local scc_vals  {
		foreach var in DamagesCoef1 DamagesCoef2  {
			outsheet `var'`scc' using ../Data/CleanedData/`var'_scc`scc'`suffix'.csv, replace nonames
		}

	foreach pollutant of local pollutants {
		outsheet DamagesCoef1_`pollutant'_`scc' using ../Data/CleanedData/DamagesCoef1`suffix'_`pollutant'_scc`scc'.csv, replace nonames
		outsheet DamagesCoef2_`pollutant'_`scc' using ../Data/CleanedData/DamagesCoef2`suffix'_`pollutant'_scc`scc'.csv, replace nonames
	}
}

save  ../Data/CleanedData/PPCapRegressors`suffix', replace 




clear


import delimited using `"../Data/electricity-generation/output/RegLoad-oge.csv"'

tab nerc

save  ../Data/CleanedData/RegLoad, replace 



clear

import delimited using `"../Data/electricity-generation/output/RegRenew-oge.csv"'

tab nerc

merge 1:1 nerc_adj utc_time using ../Data/CleanedData/RegLoad

sort nerc_adj utc_time

bysort nerc_adj: gen time=_n

keep if time<=8760 // only keep 1 year for structural model



gen LoadRaw=excess_load+load_nd

gen RenProd=load_nd

egen nerc_code=group(nerc_adj)

tab nerc_code
tab nerc_adj

sum nerc_code


local nnerc=r(max)

gen RenProdL=3*load_solar+1.45*load_wind
gen RenProdM=4.5*load_solar+1.5*load_wind
gen RenProdH=6*load_solar+1.55*load_wind

sum RenProd*



forvalues n_i=1/`nnerc' {

preserve

keep if nerc_code==`n_i'

foreach var in LoadRaw RenProd RenProdL RenProdM RenProdH  {
	outsheet `var' using ../Data/CleanedData/`var'_r`n_i'.csv, replace nonames
}

restore

}



stop

// county sunlight profiles

clear

import delimited using `"../Data/electricity-generation/output/solar-profile-county-dt.csv"'

// keep only counties included elsewhere

gen county_code=geoid

merge m:1 county_code using ../Data/CleanedData/county_i_xxwalk

keep if _merge==3

keep county_code  utc_time elec_gen // tot_elec elec_profile

bysort county_code: gen obnum=_n

drop if obnum<=10 // don't have first ten hours of year in PP data.  very little sunlight at this time

reshape wide elec_gen, i(utc_time) j(county_code)

keep elec_gen*

outsheet  using ../Data/CleanedData/CountySunlightProfileT.csv, replace nonames comma

